package com.jzg.sys.common.rest;


import com.alibaba.fastjson.JSONObject;
import com.jzg.sys.dao.entity.SecUser;
import com.jzg.sys.manager.BaseCacheManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

public class BaseRest {

    @Autowired
    private BaseCacheManager cacheService;
    @Value("${cus.sysCode}")
    public String sysCode;

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new SpecialDateEditor());
//        binder.registerCustomEditor(int.class, new CustomNumberEditor(int.class, true));  
//        binder.registerCustomEditor(int.class, new IntegerEditor());  
//        binder.registerCustomEditor(long.class, new CustomNumberEditor(long.class, true));
//        binder.registerCustomEditor(long.class, new LongEditor());  
//        binder.registerCustomEditor(double.class, new DoubleEditor());  
//        binder.registerCustomEditor(float.class, new FloatEditor());  
    }

    public String getCurrUserId() {
        HttpServletRequest req = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = req.getHeader("token");
//        if (StringUtils.isBlank(token)) {
//            throw new UnauthorizedException("token为空");
//        }
        Object obj = cacheService.get(token, sysCode);
//        if (obj == null) {
//            throw new UnauthorizedException("token过期");
//        }
        JSONObject userObj = (JSONObject) obj;
        return userObj.getString("userId");
    }


    public String getCurrUserName() {
        HttpServletRequest req = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = req.getHeader("token");
//        if (StringUtils.isBlank(token)) {
//            throw new UnauthorizedException("token为空");
//        }
        Object obj = cacheService.get(token, sysCode);
//        if (obj == null) {
//            throw new UnauthorizedException("token过期");
//        }
        JSONObject userObj = (JSONObject) obj;
        SecUser user = userObj.getObject("user", SecUser.class);
        return user.getUserName();
    }


    public SecUser getCurrUser() {
        HttpServletRequest req = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = req.getHeader("token");
//        if (StringUtils.isBlank(token)) {
//            throw new UnauthorizedException("token为空");
//        }
        return getCurrUser(token);
    }

    public SecUser getCurrUser(String token) {
        Object obj = cacheService.get(token, sysCode);
//        if (obj == null) {
//            throw new UnauthorizedException("token过期");
//        }
        JSONObject userObj = (JSONObject) obj;
        SecUser user = userObj.getObject("user", SecUser.class);
        return user;
    }


    protected String getSysCode() {
        return sysCode;
    }

}
